package example.avro

import java.io.File

import org.apache.avro.generic.GenericDatumWriter

/**
 *
 * @author tzp
 * @since 2021/8/10
 */
object TestAvroEnum {
  def main(args: Array[String]): Unit = {
    val a = "1111111"
    println(s"fadfas$a")
    testWrite
    testRead

  }

  def testWrite: Unit = {
    val employee = Employee.newBuilder.
      setName("Charlie").setFavoriteColor("blue").
      setFavoriteNumber(null).setReason(ReasonType.ADMIN_SITE_CATEGORY_FILTER).
      build
    println(ReasonType.ADMIN_SITE_CATEGORY_FILTER.toString)

    import org.apache.avro.file.DataFileWriter


//    val userDatumWriter = new SpecificDatumWriter[Employee](classOf[Employee])
    val userDatumWriter = new GenericDatumWriter[Employee](Employee.getClassSchema)
    val dataFileWriter = new DataFileWriter[Employee](userDatumWriter)
    dataFileWriter.create(employee.getSchema, new File("employees.avro"));
    dataFileWriter.append(employee)
    dataFileWriter.flush()
    dataFileWriter.close()
  }

  def testRead: Unit = {
    import org.apache.avro.file.DataFileReader
    import org.apache.avro.specific.SpecificDatumReader

    val userDatumReader = new SpecificDatumReader[Employee](classOf[Employee])
    val dataFileReader = new DataFileReader[Employee](new File("employees.avro"),
      userDatumReader)
    var e: Employee = null
    while (dataFileReader.hasNext) {
      e = dataFileReader.next(e)
      println(e)
    }
  }
}
